<?php
 
class DateHelper {
    
    public static function fuzzyDate($timestamp) {
        $myDays = array("Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat");
        if( preg_match("/[-\/:]/", $timestamp) )
            $timestamp = strtotime($timestamp);
    
        if($timestamp > time()) 
            // All future dates
            return date('m/d/y', $timestamp);
        elseif($timestamp >= mktime(0,0,0))
            // Today
            return 'Today';
        elseif($timestamp >= mktime(0,0,0) - 86400) 
            // Yesterday
            return 'Yesterday';
        elseif($timestamp >= mktime(0,0,0) - 86400*7)
            // Within 7 days
            return $myDays[date('w', $timestamp)];
        elseif($timestamp >= mktime(0,0,0,1,1))
            // Within 1 year
            return date('M jS', $timestamp);
        else
            // Older than 1 year
            return date('M jS, \'y', $timestamp);
    }
    
    public static function fuzzyDateDetailed($timestamp) {
        $myDays = array("Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat");
        if (preg_match("/[-\/:]/", $timestamp)) {
            $timestamp = strtotime($timestamp);
        }
        
        $now = time();
        $diff = $now - $timestamp;
        
        // TODO Future dates?
        
        if ($diff < 60) {
            return "Just now";
        }
        else if ($diff < (60 * 60)) {
            return floor($diff / 60) . " minutes ago";
        }
        else if ($diff < (24 * 60 * 60)) {
            return floor($diff / (60 * 60)) . " hours ago";
        }
        else if ($diff < (7 * 24 * 60 * 60)) {
            return floor($diff / (24 * 60 * 60)) . " days ago";
        }
        else if ($diff < (365 * 24 * 60 * 60)) {
            return date('M jS', $timestamp);
        }
        else {
            return date('M jS, \'y', $timestamp);
        }
    }
    
    public static function fuzzyDateDayOnly($timestamp) {
        if( preg_match("/[-\/:]/", $timestamp) )
            $timestamp = strtotime($timestamp);
        
        $diff = time() - $timestamp;
        
        if ($timestamp >= mktime(0,0,0)) {
            return 'Today';
        }
        else if ($timestamp >= mktime(0,0,0) - 86400) {
            return 'Yesterday';
        }
        else if ($diff < (7 * 24 * 60 * 60)) {
            return floor($diff / (24 * 60 * 60)) . " days ago";
        }
        else if ($diff < (365 * 24 * 60 * 60)) {
            return date('M jS', $timestamp);
        }
        else {
            return date('M jS, \'y', $timestamp);
        }
    }
}